home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 084 / blitlab / blit.tex (.txt) < prev    next >
LaTeX Document  |  1995-03-13  |  38KB  |  700 lines

  1. %   Blitter manual
  2. %\magnification=\magstep1
  3. %   We need a macro to do double column output.  These are lifted
  4. %   from the TeXbook, with only minor modifications.
  5. \newdimen\fullsize\fullsize=7.2truein\hoffset=-0.35truein
  6. \hsize=3.45truein  % this is awfully narrow; will it work?
  7. \vsize=9truein
  8. \def\makefootline{\baselineskip24pt\hbox to\fullsize{\the\footline}}
  9. \let\lr=L \newbox\leftcolumn
  10. \output={\if L\lr
  11.   \global\setbox\leftcolumn=\columnbox \global\let\lr=R
  12.  \else\doubleformat\global\let\lr=L\fi
  13.  \ifnum\outputpenalty>-20000\else\dosupereject\fi}
  14. \def\doubleformat{\shipout\vbox{\makeheadline
  15.    \hbox to\fullsize{\box\leftcolumn\hfil\columnbox}
  16.    \makefootline}
  17.  \advancepageno}
  18. \def\columnbox{\leftline{\pagebody}}
  19. %   And now, some more typical macros.
  20. %\special{landscape()}
  21. \raggedbottom
  22. \def\section#1\\{\goodbreak\vskip\baselineskip\leftline{\bf #1}\vb}
  23. \def\vb{\vskip\baselineskip}
  24. \def\b #1(){{\bf #1()}}
  25. \def\not{$\neg$}
  26. \def\xor{$\oplus$}
  27. %   Normally ~ is a tie.  We need it often enough to imply logical
  28. %   negation that we redefine it, making it an active character.
  29. \catcode`\~=\active\let~=\not
  30. \def\reg#1/{{\tt #1\space}}
  31. %   Some verbatim macros:
  32. {\obeyspaces\gdef {\ }}
  33. \def\begverb#1 {\goodbreak\vskip\baselineskip
  34. \begingroup\def\par{\leavevmode\endgraf}%
  35. \catcode`\\=12\catcode`\{=12
  36. \catcode`\}=12\catcode`\$=12\catcode`\&=12
  37. \catcode`\#=12\catcode`\%=12\catcode`\~=12
  38. \catcode`\_=12\catcode`\^=12\obeyspaces\obeylines\tt
  39. \parindent=0pt\catcode#1=0}
  40. \def\endverb{\par\endgroup}
  41. %   Now the report itself.
  42. \ \vb
  43. \centerline{\bf BlitLab and the Amiga Blitter}
  44. \centerline{Tomas Rokicki}
  45. \centerline{21 May 1987}
  46. %   Prefatory apology
  47. \section Preface\\
  48. Due to time constraints, this is not yet a complete report.  There are
  49. still a couple of things which are sketchy.  But pretend it is complete;
  50. complain about missing items so I can add them; correct any inaccuracies
  51. in the report.
  52. This manual will not make much sense without
  53. a working copy of BlitLab, as most
  54. of the documentation is in the examples provided.  So, if you don't have
  55. it yet, go out and get it.  It is on disk 69 of Fred Fish's Freely
  56. Redistributable Software Library.  Or, just ask me for a copy.
  57. %   Introduction
  58. \section Introduction\\
  59. So you have pored over the Hardware Manual and the ROM Kernel Manual, and
  60. you cannot find the information you need on the blitter.  Well, never
  61. fear; all the information you should ever need about the blitter is
  62. contained in this one handy document.
  63. All information below was derived from the Hardware Manual, ROM Kernel
  64. Manual, and a lot of empirical testing.  Using the blitter directly,
  65. as described in this report, however, bypasses the layers library.
  66. If you want to use these techniques for graphics,
  67. open your own custom screen; if you open any windows on this screen,
  68. be careful to not destroy the graphics rendered by Intuition.
  69. %   Introduction to the Hardware
  70. \section The Hardware\\
  71. The blitter comprises part of the Agnes chip in the Amiga, and can only
  72. access the lower 512K (chip) memory.  To the 68000, it appears as a
  73. set of approximately twenty sixteen bit write only registers.  It can
  74. use memory at twice the bandwidth of the 68000, or 3.6 megabytes per
  75. second (although, as we shall see, it doesn't always run this fast.)
  76. Any video memory accesses can slow the blitter down, whether for
  77. screen refresh or for the 68000.  For instance, the standard two bit
  78. deep high resolution workbench screen can slow the blitter down by
  79. approximately 30\%.  A low resolution single bit plane screen can slow it
  80. down by about 8\%.  A high resolution four bit plane screen can slow down
  81. the blitter by about 60\%.  The blitter is so fast, however, that even
  82. with this handicap it performs its tasks many times faster than the 68000.
  83. The first thing a programmer of this chip must realize is that
  84. the Amiga blitter is not a `bit' blitter; rather, it operates on words.
  85. With the appropriate programming, it can manipulate arbitrary bit
  86. rectangles.  This fact must be kept in mind when programming the blitter.
  87. The blitter uses four DMA channels to perform its work; these are called
  88. A, B, and C (sources) and D (destination).  Any or all of them may be
  89. disabled independently.  The destination can be calculated from any of
  90. 256 possible logical equations on A, B, and C.  The A and B sources can
  91. be shifted up to 15 bits to the right, and the first and last word in a
  92. line from the A source can be masked by a constant.  Each of the four
  93. channels has its own modulo.  The blitter also has an area fill and a
  94. line draw mode.
  95. %   BlitLab
  96. \section BlitLab\\
  97. To allow easy experimentation with the blitter, I have written a program
  98. called BlitLab.  This program provides a laboratory in which you can
  99. play with the blitter registers in a safe manner.  It is over 1300 lines
  100. of code that I wrote in under two days, so beware of any bugs.
  101. The blitter has a much greater potential for damage to system memory
  102. than the 68000 does, since once started it performs operations on large
  103. areas of memory without interruption or instruction checking.  If the
  104. 68000 starts executing random data as instructions, it usually very
  105. quickly executes an odd-address or illegal op-code trap.  The blitter,
  106. on the other hand, could easily wipe out kilobytes of data before the
  107. system noticed anything was amok.  BlitLab therefore carefully checks
  108. the values you have entered to be sure that system memory will not be
  109. overwritten.  Only if it will not be are you allowed to do the blit.
  110. That is, except for line mode; it is so complicated to check for line
  111. mode validity, that I let you trash memory with it.  But more on that
  112. later.
  113. %   Getting Access to the Blitter
  114. \section Getting Access\\
  115. There are currently four ways you can use the blitter.  Some work better
  116. than others.  The first way is to use the standard ROM Kernel routines
  117. for graphics.  This is the simplest and most reliable method; future
  118. blitters and operating systems will not disrupt your code.  I am not
  119. going to discuss this approach here, because I don't want to, and all of
  120. that information is in the ROM Kernel Manuals.  The second method is
  121. to arbitrarily write to the blitter registers, ignoring Intuition
  122. and friends.  This is a good way to make enemies; you can trash disks
  123. as well as system memory, but it makes for good laughs on those slow
  124. winter nights.  Just pop some random values into those blitter registers,
  125. and watch the pyrotechnics fly!
  126. The third method is a variation of the second, but you politely request
  127. permission from Intuition first, by calling \b OwnBlitter().  This routine
  128. notifies the Amiga that you want exclusive access to the blitter, and you
  129. don't want anyone else playing with it.  After this
  130. call returns, you can almost use the blitter.  Unfortunately, someone
  131. else may have already given the blitter something to do that hasn't completed;
  132. therefore, you should call \b WaitBlit() before actually mucking with
  133. the registers.  This second routine blocks until the blitter is actually
  134. finished with its work.  Once \b WaitBlit() returns, you are free to do
  135. what you like with the blitter.
  136. While you have the blitter, you must remember that Intuition cannot
  137. use it.  Therefore, \b Text() calls will not work, and your debug
  138. printf's will block if they are written to the screen, for instance.
  139. The blitter is used for disk I/O and most user interaction like gadgets,
  140. so tying
  141. up the blitter for long periods of time (longer than, say, a few
  142. milliseconds) is considered highly unfriendly.  Tying up the blitter
  143. for a second or more is grounds for lynching.
  144. When you are finished with the blitter, you should call \b DisownBlitter(),
  145. to allow Intuition to do what it likes.  Remember, however, that
  146. \b DisownBlitter() might return before the blitter is finished with your
  147. last operation, so before you use any data created by the blitter,
  148. call \b WaitBlit(), to allow the blitter to finish.
  149. This last point is worth rereading, as it is often
  150. the source of some subtle bugs.
  151. Thus, your code might look like the following:
  152. \begverb{`\$}
  153. OwnBlit() ;
  154. WaitBlit() ;
  155.  *   here you can muck with the blitter
  156.  *   until it falls off . . .
  157. draw_my_polygons() ; /* for example */
  158. DisownBlitter() ;
  159.  *   Now we want to examine the memory region
  160.  *   the blitter played with.
  161. WaitBlit() ;
  162. copy_to_disk() ;    /* for example */
  163. $endverb
  164. %   QBSBlit   add more here at some later date
  165. There is another way to gain access to the blitter; you can use the
  166. supplied blitter queue routines.  This is described (perhaps inadequately)
  167. in the ROM Kernel Manual, Volume 1, pages 2-62 through 2-65.  As of yet,
  168. I haven't had reason to use these routines; perhaps a later edition of
  169. this manuscript will have information on them.
  170. %   DMA channels
  171. \section DMA Channels\\
  172. So now we have control of the blitter; we can write to all of its
  173. registers and do whatever we like.  Before we get into exactly what
  174. we can do, let me describe the blitter DMA channels.
  175. As mentioned, the blitter has four DMA channels, A, B, C, and D.
  176. These are shown in the lower right hand corner of the BlitLab display.
  177. What, you say you are not in BlitLab yet?  Go to your Amiga, plug in
  178. a disk with BlitLab on it, and {\tt run blitlab}.  The rest of this
  179. manual assumes that you have done this.  If you are running an interlaced
  180. screen or odd colors, you might find the BlitLab display visually jarring;
  181. I recommend rebooting with the default WorkBench configuration and running
  182. BlitLab from there.
  183. Back to the DMA channels.
  184. The first three are always sources, the last is always a destination.
  185. You can use any combination of the four channels, from none of them
  186. to all four.  Each of the four channels has an 18 bit address pointer which
  187. points to the memory it will use or modify.  The least significant bit
  188. of the low order word, and the most significant fifteen bits of the high
  189. order word are ignored; this leaves 18 bits of a 32-bit pointer.
  190. Each channel also has an independent 15 bit
  191. signed modulo (in bytes, with the least significant bit again ignored.)
  192. For the three source DMA channels, there is also a data register which
  193. you can preload with constant data if the DMA channel is turned off.
  194. The DMA channels share a width and a height register.  The
  195. width is in 16 bit words, and can take a value from 1 to 64.  The height
  196. is in pixels, and can take a value from 1 to 1024.  Thus, the largest
  197. rectangular field on which the blitter can directly operate is 1024 by
  198. 1024.  However, larger fields can be handled by splitting a blit into
  199. smaller blits, and using the modulo fields appropriately.
  200. A key thing to remember here is that the width is in words, the modulos
  201. and pointers are in bytes, and the height is in pixels.  You must remember
  202. this.
  203. \section Block Clear\\
  204. The destination can receive any logical combination of its three source
  205. operands.  Let's start experimenting.  First, we'll try to clear memory.
  206. First we will try to clear memory.  The large black rectangle in the upper
  207. left hand corner of the BlitLab window is the bit region we can experiment with.
  208. Let's set some random bits to make sure clear is working.  Select the gadget
  209. currently marked `Clear'; it should toggle and be marked `Set'.  Now, move
  210. the mouse into the black rectangle, hold down the left mouse button, and
  211. move the mouse around.  Set pixels until you tire of the novelty.  Note
  212. that as you move the mouse, the Adrs and Shift fields change; these will
  213. be useful in a second.
  214. Once you have a reasonable number of pixels on the screen, you are ready
  215. to begin.  To enter data into a string gadget, select the string gadget,
  216. backspace over the old data,
  217. type new data, and then hit return.  First, in the gadget marked W,
  218. enter the number 6.  The field we are working on is 96 pixels wide by
  219. 32 pixels high; that is six words by 32 rows.  Enter 32 into the H gadget.
  220. Now, turn on the D DMA channel by selecting the gadget marked `N' in
  221. the D row; it
  222. should toggle and show `Y'.  Enter M into the PT column; this is the
  223. symbolic name for the address of the rectangle we are experimenting
  224. with.  Enter 0 into the `Function' gadget near the center of the window.
  225. We are ready to go.  At this point, select the `Calc' gadget.
  226. BlitLab will read the values you have entered and make sure that you
  227. are not going to clobber the system.  If the blit is safe, it will do
  228. nothing; otherwise it will print an error message on the window title
  229. bar.  If you get the error message, it is likely that your machine will
  230. crash if you ask it to perform the blit.
  231. Otherwise, go ahead and select the `GO' gadget.  The pixels you so
  232. laboriously set should disappear.  Congratulations, you have performed
  233. your first blit!
  234. You may have noticed that the pixels actually disappeared rather
  235. slowly.  If this is the case, you have a defective Amiga.  No, actually
  236. this is an artifact of the program; BlitLab is performing the blits
  237. in some other memory somewhere, and then copying and expanding the bits
  238. to the rectangle displayed on the screen.  It is this updating and
  239. expanding that is slow, not the blit.  So don't be alarmed.
  240. Before we get steeped in the explanations, let's experiment some more.
  241. Set the `Function' to 255, and hit `GO' again.  This should set all of
  242. the pixels in our rectangle.  Set it back to 0, and set the height to
  243. 16.  Now, only half of the rectangle is cleared.  Set the modulo (for
  244. the D channel) to 6 (bytes), and reduce the width to 3 (words, remember?)
  245. Set the `Function'
  246. back to 255, and `GO'.  Now the upper left corner of the rectangle is
  247. cleared.
  248. Oops, we are probably getting ahead of ourselves here.  For now, just
  249. take my word for the fact that setting the function to 0 clears the
  250. destination, and setting the function to 255 sets all of the bits in
  251. the destination.  I am sure the width and height (that's the W and H
  252. gadgets) explain themselves well enough, as does the address pointer
  253. (that's PT.)  But, how is the modulo interpreted?
  254. The algorithm the blitter uses to do a blit looks something like this:
  255. \begverb{`\$}
  256. doblit(daddress, height, width, dmodulo)
  257. char *daddress ;
  258. int height, width, dmodulo ;
  259.    int i, j ;
  260.     for (j=0; j<height; j++) {
  261.       for (i=0; i<width; i++) {
  262.          *daddress = function() ;
  263.          daddress += 2 ;
  264.       }
  265.       daddress += dmodulo ;
  266. $endverb
  267. Here \b function() calculates the data to be stored; it will be described
  268. later.  A row consists of `width' 16 bit words, each of which are modified
  269. in turn, incrementing the address pointer to the next word.
  270. After a row, the value `dmodulo' is added to the address.  Thus, if your
  271. bitmap is `n' words wide, $2*\hbox{width}+\hbox{dmodulo}$ should always
  272. equal `n'.
  273. \section Memory Copy\\
  274. Clear the rectangle again.  This time do it with the mouse; select the
  275. gadget marked `Point', it should toggle to `Box'.  If the gadget below
  276. this says `Set', toggle it to `Clear'.  Now, move the mouse
  277. to the upper left corner of the rectangle, press the left mouse button,
  278. and drag the mouse to the lower right corner of the rectangle, holding
  279. the mouse button down.  Then release the mouse button; the rectangle
  280. should clear.  If it doesn't clear, do it again, being careful where you
  281. press and release the mouse.  Now we are going to start experimenting
  282. with using multiple DMA channels.
  283. Turn on the A DMA channel, and set its pointer to `M'.  Set the D
  284. channel pointer to `M+192'; this is the bottom half of the rectangle.
  285. If you move the mouse to the middle of the left edge of the rectangle,
  286. within the rectangle, the Adrs field should display `M+192'; this is
  287. an easy way to find the address of a particular portion of the
  288. rectangle.  Set the modulus of the D channel back to 0, set the W field
  289. to 6, and the H field to 16.  Set the `Function' to `A'.  Now, draw
  290. some random pixels in the upper half of the rectangle.  (You will need
  291. to toggle both the `Box' and `Clear' gadgets to do this.)  Then, hit
  292. `GO'.  The upper half of the rectangle should be duplicated in the
  293. lower half!  Try setting the function to `~A' (that's tilde A
  294. on the Amiga keyboard), and see what happens.  You are actually copying
  295. memory!
  296. \section Setting Memory to a Particular Value\\
  297. You can also set memory to a particular value.
  298. Turn off the A DMA channel, but leave the
  299. function set to A.  Now put the value \$5555 in the ADAT gadget (type
  300. the dollar sign, please.)  Hit `GO'.  You should get stripes in the
  301. lower portion of the rectangle.  What is happening here is that the
  302. A DMA channel is turned off, so no memory is being loaded into the A
  303. channel.  Instead, the value in the A data register is being used.
  304. This is a quick way to set a memory region to a particular value.
  305. The important point to remember here is that you can use a
  306. channel as a constant if you turn it off and preload its data register.
  307. \section More Complex Operations\\
  308. Let's try some more complex operations on memory, now.  We are going
  309. to divide our rectangular region into four areas, one for A, one for
  310. B, one for C, and one for D.  Set W to 3, all four modulus values to
  311. 6, and the PT gadgets for A, B, C, and D to M, M+6, M+192, and M+198,
  312. respectively.  Now draw some random things in the regions corresponding
  313. to A, B, and C.  I recommend, for instance, filling the left half of
  314. A, the top half of B, and a smaller rectangle in the middle of C.
  315. You can use the `Box' and `Set' drawing modes.  Turn all four DMA
  316. channels on, and set the function to 
  317. ABC+A~A~B+~AB~C+~A~BC.  Execute `GO'; there
  318. should be eight distinct regions.  (The function is equivalent to
  319. A\xor B\xor C).
  320. Now you can experiment with the various possible functions.  Try the
  321. function AB; is the result only those bits that have both A and B set?
  322. Now try A+B, this time either A or B set should result in a destination
  323. bit on.  And so on and so forth.
  324. So how are these function codes computed?  Actually, it is quite simple.
  325. (Have you noticed how I tend to overuse the word `actually'?  I suppose
  326. that is what comes of being an engineer.)
  327. As you enter a new function, note how the least significant four nybbles
  328. of CON0 (lower left box on the display) change; this is the hexadecimal
  329. representation of the equation you entered.  You must write your function
  330. as a sum of products; the products have the values:
  331. $$\vbox{\halign{\hfil#\quad&{\tt #}\qquad&\hfil#\quad&{\tt #}\qquad&
  332. \hfil#\quad&{\tt #}\cr
  333. A&F0&B&CC&C&AA\cr
  334. ~A&0F&~B&33&~C&55\cr
  335. AB&C0&AC&A0&BC&88\cr
  336. A~B&30&A~C&50&B~C&44\cr
  337. ~AB&0C&~AC&0A&~BC&22\cr
  338. ~A~B&03&~A~C&05&~B~C&11\cr
  339. ABC&80&AB~C&40&A~BC&20\cr
  340. ~ABC&08&A~B~C&10&~AB~C&04\cr
  341. ~A~BC&02&~A~B~C&01\cr}}$$
  342. To sum them, simply `or' them.  (Do not add them.)  Thus, A\xor B is
  343. A~B+~AB or {\tt 30}$\vee${\tt 0C} or {\tt 3C}.  It is usually easier just to
  344. enter the equation into BlitLab and read the result, however.
  345. \section Shifts and Masks\\
  346. After you have had your fill of experimenting with the logic equations, we
  347. can proceed to shifts and masks.  Both the A and B DMA channel have
  348. independent shifts.  In addition, the A channel has a first word and last
  349. word mask.  These are essential in making our word blitter appear to
  350. be an actual bit blitter.  First, using the same set-up you had for the
  351. previous experiment, set the A shift to 3.  (The left half of the A
  352. region, top half of the B region, and a middle section of the C region
  353. should be set at this point.)  Shifts values are always to the
  354. right.  Set the function to our good old ABC+A~B~C+~AB~C+~A~BC, and `GO'.
  355. You should notice the shift of the A operand, and also notice that zeros
  356. are shifted in from the left.  Now reset the A shift to 0, and set the
  357. B shift to 3.  You should get some strange results; there are a few pixels
  358. that should be set, but aren't, and a few set that should be clear.
  359. Yes, your Amiga is
  360. working.  Let's examine exactly what's happening here.  (We are also going
  361. to update our blitter algorithm.)
  362. To illustrate the `problem' best, fill the top half of the A region, and
  363. clear the bottom half.  Set the function equal to `A', and set the A shift
  364. to 3.  Hit `GO', and let me explain what you have on your screen.
  365. The destination looks exactly like the source, except the first three pixels
  366. in the top row are clear, and the first three pixels in the first row
  367. which is supposed to be totally clear are set!  This is because of the way
  368. the blitter shifts.
  369. There is an internal register which holds bits between data fetches for
  370. the A and B registers.  This register is initialized to all zeros at the
  371. beginning of a blit (it is not the Data register, which is ignored on
  372. all blits with that particular DMA channel turned on.)  As a word is fetched,
  373. it is shifted to the right by the number of bits in the shift count.
  374. The high bits come from this internal data register, and the low bits
  375. are shifted out into the internal data register.  In other words, the
  376. algorithm looks something like this:
  377. \begverb{`\$}
  378. doblit(aaddress, height, width, amodulo, sh)
  379. char *aaddress ;
  380. int height, width, amodulo, sh ;
  381.    int i, j ;
  382.    int prev_data, data ;
  383.     prev_data = 0 ;
  384.    for (j=0; j<height; j++) {
  385.       for (i=0; i<width; i++) {
  386.          data = ((prev_data << 16)
  387.                 | *aaddress) >> sh ;
  388.          prev_data = *aaddress ;
  389.          function(data) ;
  390.          aaddress += 2 ;
  391.       }
  392.       aaddress += amodulo ;
  393. $endverb
  394. So, as you can see, things work nicely along the row.  The first time
  395. around, the most significant bits of the data word get shifted right
  396. and used.  The next operation uses the least significant bits of the previous
  397. data word and the most significant bits of the current word, as it is
  398. supposed to.  The only difficulty appears across rows.  For the first
  399. word in a subsequent row, the low order bits of the last word in the
  400. previous row are used, rather than shifting in zeros as happens on the first
  401. row.  Things start to get a bit hairier.
  402. But all is not lost.  Using the A register's ability to mask the first
  403. and last word in a row, we can zero out any words we want, even if we
  404. turn off the A DMA channel.  For instance, using the current settings of
  405. BlitLab, set the least significant three bits of the ALWM to 0.  (Note
  406. that {\it these} are the bits we need to mask out; the masks are applied before
  407. the shifting.  In addition, the FWM is applied only to the first word in
  408. each row; the LWM is applied only to the last word; if they are the same
  409. word, both masks are applied.)  Redo the blit, and now things
  410. appear to be working correctly.
  411. \section Copying Arbitrary Regions\\
  412. We will now attempt to move a rectangular array of bits from one random
  413. bit location to another.
  414. To set things up, clear out the entire rectangle.  Now, draw an ellipse
  415. in the upper half region; try to get it close to the four borders.
  416. Set the entire lower region, and reset an X across the entire region.
  417. Reset all of the modulos to 0, and the width to 6.  Set the A and B source
  418. addresses to M, and the C and D addresses to M+192.  We shall attempt to
  419. move the portion of the ellipse from bit positions 2 through 28 to bit
  420. positions 13 through 39 in the destination, leaving all the other bits
  421. in the destination unchanged.  The source spans only two words, but
  422. the destination spans three, so the blit width must be 3.  For some
  423. other blits, the source might span more words than the destination;
  424. the width must always be the maximum of the two.  So set the width to
  425. 3, and all modulos to 6.
  426. The A channel is going to function as a mask.  Wherever the bit in the
  427. A channel is set, we will copy the source to the destination; where the
  428. bits are clear, the destination must not be changed.
  429. The B channel will be used to actually fetch the source bits,
  430. the C channel will
  431. read the destination, because we will need to merge the destination
  432. with the new source before writing, and the D channel will do the writing.
  433. So, set the B address to M, and the C and D addresses to M+192.  Turn
  434. off the A channel, but turn all three others on.  Set the A data to
  435. \$FFFF, and the B shift to 11.  Note that we are using the A channel
  436. as a constant; even though it is a constant, the mask registers will
  437. still mask out bits of that constant.
  438. Now we need to figure out how we are going to get A to mask out only
  439. those bits we need to change.  Since our destination is the one which
  440. spans three words, A must track it, so set the shift of A to zero,
  441. and the FWM to zero for those bits of the first word of the destination
  442. which are to be left alone.  In our case, we wish to leave the first 13
  443. bits alone, so we use a value of \$0007.  The LWM gets set to zero for
  444. the bits of the last word, similarly, yielding a value of \$FF00.
  445. Note that if the source were three words and the destination two, the
  446. A channel would have to track B.  In this case, you would set the shift
  447. for A the same as the shift for B, and set the masks to mask out the
  448. particular bits in B which will not be used.  We are almost ready.
  449. Let's think about the function we need now.  For where the A bits are
  450. masked out, or zero, we need to leave the destination alone; this gives
  451. us the minterm ~AC.  Where A bits are set, we pass B through unchanged;
  452. this gives us AB.  Summing these, we enter AB+~AC for our function, and
  453. hit `GO'.  Carefully check out the picture; it should have worked.
  454. We can also complement on the copy using the function A~B+~AC.  The
  455. function AB+C provides an `or' draw, and the function AB~C+A~BC+~AC
  456. provides an exclusive or copy.  You might try these.
  457. Whew!  That's a lot.  You might take a breather here.  Then, later,
  458. come back and reread the previous paragraphs, and play with BlitLab
  459. some more.  It's really quite simple once you get the hang of it.
  460. It is interesting to note that it required the full functionality of
  461. the blitter---all four channels, shifts and masks---just to do an
  462. arbitrary bit rectangle copy.  These are the difficulties you run into
  463. when trying to make a word blitter perform as a bit blitter.
  464. \section Decrement Mode\\
  465. Sometimes the source and destination of your blits will overlap.  If
  466. the destination comes at a lower memory address than the source, everything
  467. will work fine.  However, if the destination comes at a higher memory
  468. address and overlaps a source, a portion of the source will be overwritten
  469. by the destination before it can be used as source, so the blit will not
  470. perform as expected.  The blitter has a special flag which solves this
  471. problem.  This flag puts the blitter in the decrement mode, where addresses
  472. are decremented instead of incremented as the blit proceeds.
  473. Toggle the gadget `(desc)', it should become
  474. `DESC', indicating that it is set.  If you use this mode, you must
  475. initialize the addresses to the end of the source or destination block,
  476. and use negative modulo values.  The W and H must stay positive.
  477. Try it.  Turn on the A and D channels, and turn off the B and C.
  478. Set the A channel to M+190, and the D channel to M+382.  Set the
  479. function to A, the modulos to 0, the width to -6, and the height to
  480. 16.  Set the FWM and LWM back to \$FFFF.
  481. Draw some random pattern in the upper half of the rectangle,
  482. insure that the DESC flag is set, and `GO'.  It should work as before.
  483. Now set D to M+202, and watch the pattern step down with each blit.
  484. %   Area Fill
  485. \section Area Fill\\
  486. Everything we have dealt with so far has been strictly data movement
  487. and strict logical equations.  Now we examine one of the more esoteric
  488. abilities of the blitter---area fills.  This feature is actually quite
  489. easy to demonstrate, but it only works in the descending mode.  Set
  490. up BlitLab as follows:  Channels A, D on; A address of M+190, D
  491. address of M+382; W of 6, H of 16, modulos of 0, function of A, DESC
  492. on.  Clear the rectangular array, and draw two vertical lines in
  493. the top half of the rectangle, separated by at least one pixel.
  494. If the lines are not exactly straight up and down, that's okay, just
  495. insure that there is only one pixel set per row per line.  (You might
  496. have to go to Point Clear mode and pick out a few pixels for this.)
  497. Turn on the `(ife)' flag (inclusive fill enable), and `GO'.  The area
  498. between the two lines should be filled in the lower portion of the
  499. rectangle.
  500. Now turn on the `(fci)' flag (fill carry in), and `GO'.  This time,
  501. the area outside the two lines are filled.  The fill carry flag is
  502. toggled for each bit seen, and if it is set, the bits in the destination
  503. are set.  Now, turn off the `(fci)' flag, and turn on the `(efe)' flag
  504. (exclusive fill enable.)  The area between the two lines is again filled,
  505. but this time the line on the trailing edge of the fill was deleted.
  506. This is useful for narrower fills.
  507. Now draw a third vertical line between the previous two, and hit `GO'.
  508. Note how the fill is indeed performed correctly, and the FCI bit is
  509. restored to its set value at the beginning of each row.  Accidentally
  510. set a random bit somewhere in A, and observe the effect is has on the
  511. fill.  Now you know why each line must be only one pixel wide.
  512. You can still perform any operation on the A, B, and C sources before
  513. the area fill; the lines in the result will be used.  For instance,
  514. you can area fill based on only a particular color of line, by setting
  515. A, B, and C to the bit planes of the display, and setting the function
  516. to one which selects only the appropriate color.
  517. %   Line Mode
  518. \section Line Drawing\\
  519. This area is the sketchiest part of my knowledge.  I have actually gotten
  520. the blitter to draw lines, but it took a lot of time and effort.  The
  521. Hardware and ROM Kernel Manuals are incorrect in some of their assertions;
  522. I had to disassemble part of the ROM to determine exactly how to draw
  523. lines.  For
  524. brevity, the following is an algorithm which will draw a line from
  525. x1, y1 to x2, y2 on a window at m which is wx by wy pixels.  X and Y
  526. are used to hold the slope values for the line, and assist in finding
  527. the quadrant the line is to be drawn in.  Note how the flags (fci),
  528. (ife), and (efe) are used to select the particular quadrant.
  529. \begverb{`\$}
  530. doline(x1, y1, x2, y2)
  531. int x1, y1, x2, y2 ;
  532.    int x, y ;
  533.    int X, Y ;
  534.    int q = 0 ;
  535.    int t ;
  536.    x = x2 - x1 ;
  537.    y = y2 - y1 ;
  538.    if (x < 0)
  539.       X = - x ;
  540.    else
  541.       X = x ;
  542.    if (y < 0)
  543.       Y = -y ;
  544.    else
  545.       Y = y ;
  546.    if (x > 0) {
  547.       if (y > 0)
  548.          q = (X > Y ? 1 : 0) ;
  549.       else
  550.          q = (X > Y ? 3 : 4) ;
  551.    } else {
  552.       if (y > 0)
  553.          q = (X > Y ? 5 : 2) ;
  554.       else
  555.          q = (X > Y ? 7 : 6) ;
  556.    if (Y > X) {
  557.       t = X ;
  558.       X = Y ;
  559.       Y = t ;
  560.    blit.height = X + 1 ;
  561.    blit.apt = 4 * Y - 2 * X ;
  562.    if (2 * Y - X < 0)
  563.       blit.sign = 1 ;
  564.    else
  565.       blit.sign = 0 ;
  566.    blit.amod = 4 * (Y - X) ;
  567.    blit.bmod = 4 * Y ;
  568.    blit.line = 1 ;
  569.    blit.efe = (q & 1) ;
  570.    blit.ife = (q & 2) >> 1 ;
  571.    blit.fci = (q & 4) >> 2 ;
  572.    blit.adat = 0x8000 ;
  573.    blit.bdat = 0xffff ;
  574.    blit.ash = x1 & 15 ;
  575.    blit.cpt = blit.dpt = m +
  576.       ((x1 >> 3) & ~1) + y * (wx >> 3) ;
  577.    blit.cmod = blit.dmod = wx >> 3 ;
  578.    blit.width = 2 ;
  579.    blit.usea = 1 ;
  580.    blit.useb = 0 ;
  581.    blit.usec = 1 ;
  582.    blit.used = 1 ;
  583. $endverb
  584. All of these calculations and initializations are done automatically by
  585. BlitLab.  All you need do is enter the starting x and y and ending x
  586. and y values into SX, SY, EX, and EY, respectively, and then hit `SETUP'.
  587. (The x values can range from 0 to 95; the y values from 0 to 31.  If you
  588. exceed these ranges, you will walk on system memory, and BlitLab won't
  589. check line mode!)
  590. We do not set the `function' variable in the above routine or in
  591. BlitLab, because there
  592. is more than one way to draw a line.  As the line is being drawn, the
  593. bit set in the A register moves across and wraps around; this is the bit
  594. that might be set.  The original destination is available from the C
  595. channel, and the B channel provides a mask.  Thus, to just draw a solid
  596. line, you would use the equation A+~AC (if A is set, draw a bit, otherwise
  597. pass the destination through unchanged.)  Try it.  To draw an exclusive
  598. or line, use A~C+~AC.  To draw a textured line, use AB+~AC, and put your
  599. texture in B.  Note how the A and B address registers are used as
  600. accumulators instead of address registers.
  601. There is also an option to draw a line with only one bit set per horizontal
  602. row; this is essential for drawing polygons to be filled later.  If you
  603. set the `(desc)' flag, the lines will be drawn this way.
  604. %   Speed
  605. \section Speed\\
  606. So, all of those fancy operations are fine and dandy, but just how fast
  607. is the blitter, anyway?  This depends entirely on which DMA channels
  608. are turned on.  You might be using a DMA channel as a constant, but unless
  609. it is turned on, it does not count against you.  The minimum blitter
  610. cycle is four clocks; the maximum is eight.  Use of the A register is
  611. always free.  Use of the B register always adds two clocks to the
  612. blitter cycle.  Use of either C or D is free, but use of both adds
  613. another two clocks.  Thus, a copy cycle, using A and D, takes four
  614. clocks per cycle; a copy cycle using B and D takes six clocks per
  615. cycle, and a generalized bit copy using B, C, and D takes eight clocks.
  616. When in line mode, each pixel takes eight clocks.
  617. The clock is the 7.18 MHz system clock.  To calculate the total time
  618. for the blit in microseconds, after setup, you use the equation
  619. $$t={nHW\over 7.18}$$
  620. where $t$ is the time in microseconds, $n$ is the number of clocks
  621. per cycle, and $H$ and $W$ are the height and width of the blit,
  622. respectively.
  623. Actually, this is a minimum time, which is strictly impossible.
  624. Display data fetches, 68000 cycles, and other operations can steal
  625. cycle bandwidth away from the blitter.  One way to eliminate most of
  626. this overhead is to call the macro {\tt OFF\_DISPLAY}
  627. which turns off the display; this is not a friendly thing to do,
  628. however.  Don't forget to call {\tt ON\_DISPLAY} after the blit
  629. is finished!
  630. \section Blitter Registers\\
  631. So far we've discussed virtually every aspect of the blitter, except
  632. exactly how its registers are organized, and how one actually stuffs
  633. these registers.  Well, the blitter is accessible from the custom
  634. hardware include file {\tt hardware/custom.h}, and makes available
  635. 20 write-only 16 bit registers, eight of which are organized as four
  636. 32 bit registers.  Full documentation is in the Hardware
  637. Reference Manual, but I've summarized them here, as they are available
  638. from C.  You might have noticed how the register contents block on
  639. the lower left hand portion of the window changes as you set various
  640. blitter parameters; this is an easy way to calculate register settings.
  641. \vb{\leftskip=\parindent\parindent=-\parindent
  642. \reg custom.bltcon0/  This sixteen bit register contains the A shift
  643. value in its top four bits and the function code in its low eight bits.
  644. Bits 8 through 11 are used to indicate which DMA channels are on; 8,
  645. 9, 10, and 11 correspond to DMA channels D, C, B, and A, respectively.
  646. \reg custom.bltcon1/  This sixteen bit register holds the B shift in
  647. its top four bits and five flags in its lower five bits.  Bits 0, 1,
  648. 2, 3, and 4 are (line), (desc), (fci), (ife), and (efe), respectively.
  649. In the line mode, bits 5 and 6 are (ovf) and (sign), respectively.
  650. \reg custom.bltafwm/  This sixteen bit register holds the first word
  651. mask for the A DMA channel.
  652. \reg custom.bltalwm/  This sixteen bit register holds the last word mask
  653. for the A DMA channel.
  654. \reg custom.bltapt/  This eighteen bit register holds the address of the
  655. A DMA channel; it is written as a byte value, but the least significant
  656. bit is ignored, as are the most significant thirteen bits.
  657. \reg custom.bltbpt/  This eighteen bit register serves as the address for
  658. the B DMA channel.
  659. \reg custom.bltcpt/  This eighteen bit register provides the address for
  660. the C DMA channel.
  661. \reg custom.bltdpt/  This eighteen bit register is the destination or D
  662. channel address.
  663. \reg custom.bltsize/  This sixteen bit register gets the height
  664. in rows, in the most significant ten bits, and the width in words, in the
  665. least significant six bits.  Note that assigning to this register starts
  666. the blitter, so it should be the last register initialized.
  667. \reg custom.bltamod/  This fifteen bit signed register holds the modulo
  668. value for the A DMA channel.  The value written is in bytes, but the least
  669. significant bit is ignored.  The most significant bit is used as a sign
  670. \reg custom.bltbmod/  This fifteen bit register serves a similar function
  671. for the B DMA channel.
  672. \reg custom.bltcmod/  This fifteen bit register provides the modulus for
  673. the C DMA channel.
  674. \reg custom.bltdmod/  This fifteen bit register holds the modulus for the
  675. D channel.
  676. \reg custom.bltadat/  This sixteen bit preloadable data register holds
  677. data for the A DMA channel.
  678. \reg custom.bltbdat/  This sixteen bit preloadable data register holds
  679. data for the B DMA channel.
  680. \reg custom.bltcdat/  This sixteen bit preloadable data register holds
  681. data for the C DMA channel.
  682. \par}
  683. \section Missing Sections\\
  684. The things we have neglected to talk about, which should be included,
  685. are listed here.  We need to describe \b QBSBlit() and \b QBlit().
  686. We also need to discuss the dirty mode of the blitter, and the zero
  687. flag that the blitter returns.  It would be nice to have some
  688. empirical timings comparing \b QBlit() with \b OwnBlitter() methods
  689. of obtaining the blitter.
  690. \section Plug for Amiga\TeX\\
  691. This manual was typeset in a hurry on the Amiga using Amiga\TeX, previewed
  692. on the screen, and printed on a QMS-Kiss at 300 dots per inch.
  693. For a demonstration disk and more information, contact Tom Rokicki
  694. at (415) 326-5312, or send mail to him at Box 2081, Stanford, CA\space\space
  695. 94305.
  696. %   No, we are not really done, but that's enough for now.  We might
  697. %   have to eject an empty half-page here.
  698. \vfill\eject
  699. \if R\lr \null\vfill\eject\fi
  700.